//
// Created by Scave on 2024/2/25.
//
#include "BasicComponents.h"

NS_RIDER_BEGIN

    Mesh::Mesh() = default;

    Mesh::~Mesh() {
        vertex_data_list.clear();
    }

    void Mesh::AddVertex(Vertex& vertex) {
        vertex_data_list.push_back(vertex);
    }

    void Mesh::AddIndex(UInt16 index) {
        vertex_index_list.push_back(index);
    }

    void Mesh::AddFace(Vertex& v1, Vertex& v2, Vertex& v3) {
        UInt16 index1 = vertex_data_list.size();
        UInt16 index2 = index1 + 1;
        UInt16 index3 = index1 + 2;
        vertex_index_list.push_back(index1);
        vertex_index_list.push_back(index2);
        vertex_index_list.push_back(index3);
        vertex_data_list.push_back(v1);
        vertex_data_list.push_back(v2);
        vertex_data_list.push_back(v3);
    }

    void Mesh::AddQuad(Vertex& bottom_left, Vertex& bottom_right, Vertex& top_right,
                       Vertex& top_left) {
        // add vertex indices,2 triangles have 6 vertices,so there are 6 indices
        UInt16 index1 = vertex_data_list.size();
        UInt16 index2 = index1 + 1;
        UInt16 index3 = index1 + 2;
        UInt16 index4 = index1 + 3;
        // first triangle
        vertex_index_list.push_back(index1);
        vertex_index_list.push_back(index2);
        vertex_index_list.push_back(index3);
        // second triangle
        vertex_index_list.push_back(index1);
        vertex_index_list.push_back(index3);
        vertex_index_list.push_back(index4);
        // add vertex data
        vertex_data_list.push_back(bottom_left);
        vertex_data_list.push_back(bottom_right);
        vertex_data_list.push_back(top_right);
        vertex_data_list.push_back(top_left);
    }

    void Mesh::SetVertices(UInt16 vertices_count, Vertex* vertices) {
        vertex_data_list.clear();
        for (UInt16 i = 0; i < vertices_count; ++i) {
            vertex_data_list.push_back(vertices[i]);
        }
    }

    void Mesh::SetIndices(UInt16 indices_count, UInt16* indices) {
        vertex_index_list.clear();
        for (UInt16 i = 0; i < indices_count; ++i) {
            vertex_index_list.push_back(indices[i]);
        }
    }

    UInt16 Mesh::VertexSize() const {
        return vertex_data_list.size();
    }

    UInt16 Mesh::IndexSize() const {
        return vertex_index_list.size();
    }

NS_RIDER_END
